﻿using System.Collections.Generic;

namespace Redbrick.Silverlight.Common
{
	public static partial class Sequence
	{

		public delegate _type listDelegate<_type>(int index);
		public delegate _type listIndexDelegate<_type>(int index, List<_type> list);

		public delegate _type arrayDelegate<_type>(int index);
		public delegate _type arrayIndexDelegate<_type>(int index, _type[] array);

		public delegate _type arrayDelegate2D<_type>(int x,int y);
		public delegate _type arrayIndexDelegate2D<_type>(int x, int y, _type[,] array);


		public delegate _type jaggedArrayDelegate<_type>(int row,int col);
		public delegate _type jaggedArrayIndexDelegate<_type>(int row,int col, _type[][] array);




		/// <summary>
		/// initialize an array of size count with value
		/// </summary>
		public static List<_type> InitList<_type>(int count, _type value)
		{
			List<_type> data = new List<_type>(count);
			for (int i = 0; i < count; i++)
				data.Add(value);
			return data;
		}
		public static List<_type> InitList<_type>(int count, listDelegate<_type> func)
		{
			List<_type> data = new List<_type>(count);
			for (int i = 0; i < count; i++)
			{
				data.Add(func(i));
			}
			return data;
		}
		public static List<_type> InitList<_type>(int count, listIndexDelegate<_type> func)
		{
			List<_type> data = new List<_type>(count);
			for (int i = 0; i < count; i++)
			{
				data.Add(func(i, data));
			}
			return data;
		}

		/// <summary>
		/// initialize an array of size count with value
		/// </summary>
		public static _type[] InitArray<_type>(int count, _type value)
		{
			_type[] data = new _type[count];
			for (int i = 0; i < count; i++)
				data[i] = value;
			return data;
		}
		public static _type[] InitArray<_type>(int count, arrayDelegate<_type> func)
		{
			_type[] data = new _type[count];
			for (int i = 0; i < count; i++)
			{
				data[i] = func(i);
			}
			return data;
		}
		public static _type[] InitArray<_type>(int count, arrayIndexDelegate<_type> func)
		{
			_type[] data = new _type[count];
			for (int i = 0; i < count; i++)
			{
				data[i] = func(i, data);
			}
			return data;
		}

		/// <summary>
		/// initialize an array of size count with value
		/// </summary>
		public static _type[,] InitArray<_type>(int width,int height, _type value)
		{
			_type[,] data = new _type[width,height];
			for (int w = 0; w < width; w++)
			{
				for (int h = 0; h < height;h++ )
					data[w,h] = value;
			}
			return data;
		}
		public static _type[,] InitArray<_type>(int width,int height, arrayDelegate2D<_type> func)
		{
			_type[,] data = new _type[width, height];
			for (int w = 0; w < width; w++)
			{
				for (int h = 0; h < height; h++)
					data[w,h] = func(w,h);
			}
			return data;
		}
		public static _type[,] InitArray<_type>(int width, int height, arrayIndexDelegate2D<_type> func)
		{
			_type[,] data = new _type[width, height];
			for (int w = 0; w < width; w++)
			{
				for (int h = 0; h < height; h++)
					data[w, h] = func(w, h,data);
			}
			return data;
		}



		public static _type[][] InitArray<_type>(int RowCount, int[] dimensions, _type value)
		{
			_type[][] data = new _type[RowCount][];

			for (int row = 0; row < RowCount; row++)
			{
				data[row] = Sequence.InitArray(dimensions[row], value);
			}
			return data;
		}
		public static _type[][] InitArray<_type>(int RowCount, int[] dimensions, jaggedArrayDelegate<_type> func)
		{
			_type[][] data = new _type[RowCount][];

			for (int row = 0; row < RowCount; row++)
			{

				data[row] = new _type[dimensions[row]];
				for (int col = 0; col < data[row].Length; col++)
					data[row][col] = func(row, col);
			}
			return data;
		}
		public static _type[][] InitArray<_type>(int RowCount, int[] dimensions, jaggedArrayIndexDelegate<_type> func)
		{
			_type[][] data = new _type[RowCount][];

			for (int row = 0; row < RowCount; row++)
			{

				data[row] = new _type[dimensions[row]];
				for (int col = 0; col < data[row].Length; col++)
					data[row][col] = func(row, col,data);
			}
			return data;
		}

	}
}
